<template>
  <div v-loading="isLoading">
    <p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/my_apps' }">
          我的APP
        </el-breadcrumb-item>
        <el-breadcrumb-item>APP详情</el-breadcrumb-item>
      </el-breadcrumb>
    </p>

    <img
      class="platform-icon"
      v-if="appBox && appBox.Platform === 'iOS'"
      src="@/assets/icon_platform_ios.svg"
      alt="iOS icon"
    />
    <img
      class="platform-icon"
      v-if="appBox && appBox.Platform === 'Android'"
      src="@/assets/icon_platform_android.svg"
      alt="Android icon"
    />
    <h1>
      {{ title }}
    </h1>

    <div v-if="hasData">
      <div class="app-box" v-if="appBox">
        <el-row>
          <el-col :xs="12" :sm="20" class="info-title"> 下载地址 </el-col>
          <el-col :xs="12" :sm="4">
            <el-button type="primary">自定义</el-button>
          </el-col>

          <el-col :span="24" :offset="1">
            <a
              target="_blank"
              :href="
                '/download/' +
                (appBox.CustomID == '' ? appBox.ID : appBox.CustomID)
              "
            >
              {{downloadURL}}
            </a>
          </el-col>
        </el-row>

        <el-row>
          <el-col :xs="12" :sm="20" class="info-title">
            访问下载页需要密码
          </el-col>
          <el-col :xs="12" :sm="4">
            <el-switch v-model="appBox.Password"></el-switch>
          </el-col>
        </el-row>
      </div>

      <div v-for="item in appBox.AppList" :key="item.ID" class="app">
        <el-row>
          <el-col :xs="24" :sm="24" :md="4">
            <div class="icon">
              <img :src="item.Icon" :alt="item.Name" />
              <p>{{ item.Name }}</p>
            </div>
          </el-col>
          <el-col :xs="24" :sm="24" :md="20">
            <div class="info">
              <download-list-app :show-title="true" :app="item" />
              <el-row>
                <el-col :offset="16">
                  <el-button type="primary" plain>编辑</el-button>
                  <el-button type="danger" plain>删除</el-button>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <el-empty v-else description="空空如也"></el-empty>
  </div>
</template>
<script>
import DateFromTimestamp from '@/tools/time-tool.js'
import DownloadListApp from '@/components/DownloadListApp.vue'

export default {
  data () {
    return {
      appBox: null,
      isLoading: false
    }
  },
  components: {
    DownloadListApp
  },
  computed: {
    hasData () {
      if (this.appBox) {
        return this.appBox.AppList.length !== 0
      } else {
        return false
      }
    },
    title () {
      if (this.appBox) {
        const first = this.appBox.AppList[0]
        if (first && first.Name) {
          return first.Name
        }
      }
      return 'APP详情'
    },
    downloadURL () {
      return window.location.origin + '/download/' +
                (this.appBox.CustomID === '' ? this.appBox.ID : this.appBox.CustomID)
    }
  },
  mounted () {
    this.getAppDetail()
  },
  methods: {
    async getAppDetail () {
      this.isLoading = true
      try {
        const Platform = this.$route.params.Platform
        const PackageName = this.$route.params.PackageName
        const result = await this.$net.get('/app/detail', { params: { Platform, PackageName } })
        this.appBox = result.data
      } finally {
        this.isLoading = false
      }
    },
    getDate (timestamp) {
      return DateFromTimestamp(timestamp)
    }
  }
}
</script>

<style lang="scss" scoped>
  .platform-icon {
    width: 60px;
    height: 60px;
  }
  .app-box {
    margin-bottom: 40px;
    box-shadow: var(--shadow);
    border-radius: 10px;
    overflow: hidden;
    padding: 20px 0;
    .el-row {
      margin: 0 20px;
      margin-bottom: 10px;
      // border-bottom: 1px solid rgb(237, 237, 237);
    }
    .el-col {
      min-height: 40px;
    }
  }
  .app {
    margin-bottom: 40px;
    box-shadow: var(--shadow);
    border-radius: 10px;
    overflow: hidden;
    transition: all 0.3s ease;
    &:hover {
      box-shadow: var(--shadow-hover);
      transform: translateY(-5px);
    }

    .icon {
      padding: 40px;
      height: calc(100% - 80px);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      background: radial-gradient(
        circle at 50%,
        rgb(83, 168, 255) 30%,
        rgb(55, 113, 172)
      );
      img {
        width: 100px;
        height: 100px;
        border-radius: 15px;
      }
      p {
        text-align: center;
        color: white;
        text-shadow: 0 0 2px rgba(0, 0, 0, 0.479);
      }
    }
    .info {
      color: var(--Text);
      margin: 40px;
      hr {
        border: 0;
        border-bottom: 1px solid var(--Placeholder);
      }
    }
  }
  .el-col {
    color: var(--SubText);
    min-height: 20px;
  }
  .info-title {
    font-weight: bold;
    &::after {
      content: ":";
    }
  }
  .app-title {
    font-size: 20px;
    font-weight: bold;
    color: var(--MainText);
  }

  pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
</style>
